package com.ibm.commerce.url.tasks.accelerator;

/**
 * Info 1
 * Info 2
 * Info 3
 * Info 4
 * Info 5
 * Info 6
 */

import com.ibm.commerce.url.harness.*;
import com.ibm.commerce.url.util.Log;
import com.ibm.commerce.url.exception.*;
import com.ibm.wc.test.exception.DatablockMissingException;

import java.util.LinkedHashMap;
import java.util.Map;

public class AcceleratorStoreAndLangSelect extends URLTestTask {
	/********************************************************
	 * Section - Input Parameter names constants
	 * 
	 * If tester doesn't know what parameters a task has he can use dot method to refer to a parameter easily, otherwise
	 * he has to use String to pass the parameter name and it's prone to error. For example:
	 * 
	 * 	String currentFulfilmentCenterID = browserContext.getOutputParam(
	 * 		TaskName.INPUT_PARAMETER_NAME_FULFILLMENTCENTERID
	 * 	);
	 * 
	 * These lines are automatically generated from task definition XML file by code generator.
	 *  
	 ********************************************************/
	public static final String INPUT_PARAMETER_NAME_STOREID = "storeId"; 
	public static final String INPUT_PARAMETER_NAME_LANGID = "langId";
	public static final String INPUT_PARAMETER_NAME_FULFILLMENTCENTERID = "fulfillmentCenterId";
	public static final String INPUT_PARAMETER_NAME_SEARCHCRITERIA = "searchCriteria";
	
	private static AcceleratorStoreAndLangSelect instance = new AcceleratorStoreAndLangSelect();
	private AcceleratorStoreAndLangSelect(){}
	
	public static void execute() throws TaskFailException, DatablockMissingException {
		instance.runTask();
	}
	public static void execute(int counterIndex) throws TaskFailException, DatablockMissingException {
		instance.runTask(counterIndex);
	}
	
	protected void executeTask() throws TaskFailException {
		acceleratorStoreAndLangSelectStep1();
		acceleratorStoreAndLangSelectStep2();
		acceleratorStoreAndLangSelectStep3();		
	}

	private static Map<String, String> acceleratorStoreAndLangSelectStep1_FORM = new LinkedHashMap<String, String>();
	static{
		acceleratorStoreAndLangSelectStep1_FORM.put("XMLFile","common.merchantCenterBMH");
		//The automatically generated task form may have some hardcodings, it's up to the testers to manually clean up them
		acceleratorStoreAndLangSelectStep1_FORM.put("storeId","");		
		acceleratorStoreAndLangSelectStep1_FORM.put("langId","");
		acceleratorStoreAndLangSelectStep1_FORM.put("time","1260942391984");
		acceleratorStoreAndLangSelectStep1_FORM.put("fulfillmentCenterId","");
		
	}
	private void acceleratorStoreAndLangSelectStep1() throws TaskFailException {
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering acceleratorStoreAndLangSelectStep1");
		
		///////// Get input from data file //////////
		putParameterInMap(acceleratorStoreAndLangSelectStep1_FORM, "storeId", getParameterFromDataFile(INPUT_PARAMETER_NAME_STOREID));
		putParameterInMap(acceleratorStoreAndLangSelectStep1_FORM, "langId", getParameterFromDataFile(INPUT_PARAMETER_NAME_LANGID));
		putParameterInMap(acceleratorStoreAndLangSelectStep1_FORM, "fulfillmentCenterId", getParameterFromDataFile(INPUT_PARAMETER_NAME_FULFILLMENTCENTERID));
				
		//hard coded here just for test
		if("512".equals(acceleratorStoreAndLangSelectStep1_FORM.get("storeId"))){
			putParameterInMap(acceleratorStoreAndLangSelectStep1_FORM, "XMLFile", "common.merchantCenterB2B");
		}
		else{
			putParameterInMap(acceleratorStoreAndLangSelectStep1_FORM, "XMLFile", "common.merchantCenterBMH");
		}	
		
		WebHit("https://standardhost/webapp/wcs/tools/servlet/MerchantCenterView", WEB_FORM_POST, acceleratorStoreAndLangSelectStep1_FORM);
			
//		putParameterInReusableMap(INPUT_PARAMETER_NAME_LANGID, acceleratorStoreAndLangSelectStep1_FORM.get("langId"));
//		putParameterInReusableMap(INPUT_PARAMETER_NAME_STOREID, acceleratorStoreAndLangSelectStep1_FORM.get("storeId"));
//		putParameterInReusableMap(INPUT_PARAMETER_NAME_FULFILLMENTCENTERID, acceleratorStoreAndLangSelectStep1_FORM.get("fulfillmentCenterId"));
		
		/********************************************************
		 *  For the values of some parameters, like storeId / langId / catalogId / fulfillmentId / storeOwnerId / storeType / currentUserId,
		 *  as they are used by many tasks so we can save them into a global map that can be shared between all tasks, without having to explicitly
		 *  pass these values task by task.
		 *  
		 *  The values of this kind of parameters are usually got from first pages, e.g. for store front tasks the 
		 *  storeId / langId / catalogId can got from StoreView task.
		 *  
		 *  These lines cannot be automatically generated by task generator, as it cannot be that intelligent.
		 ********************************************************/
		putParameterInGlobalMap(INPUT_PARAMETER_NAME_LANGID, acceleratorStoreAndLangSelectStep1_FORM.get("langId"));
		putParameterInGlobalMap(INPUT_PARAMETER_NAME_STOREID, acceleratorStoreAndLangSelectStep1_FORM.get("storeId"));
		putParameterInGlobalMap(INPUT_PARAMETER_NAME_FULFILLMENTCENTERID, acceleratorStoreAndLangSelectStep1_FORM.get("fulfillmentCenterId"));
		
		
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting acceleratorStoreAndLangSelectStep1");		
	}
	
	private static Map<String, String> acceleratorStoreAndLangSelectStep2_FORM = new LinkedHashMap<String, String>();
	static{
		acceleratorStoreAndLangSelectStep2_FORM.put("langId","");
		acceleratorStoreAndLangSelectStep2_FORM.put("storeId","");
	}
	private void acceleratorStoreAndLangSelectStep2() throws TaskFailException{
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering acceleratorStoreAndLangSelectStep2");
		
		putParameterInMap(acceleratorStoreAndLangSelectStep2_FORM, "langId", getParameterFromGlobalMap(INPUT_PARAMETER_NAME_LANGID));
		putParameterInMap(acceleratorStoreAndLangSelectStep2_FORM, "storeId", getParameterFromGlobalMap(INPUT_PARAMETER_NAME_STOREID));
		putParameterInMap(acceleratorStoreAndLangSelectStep2_FORM, "fulfillmentCenterId", getParameterFromGlobalMap(INPUT_PARAMETER_NAME_FULFILLMENTCENTERID));
		
		WebHit("https://standardhost/webapp/wcs/tools/servlet/MCLaunched", WEB_FORM_GET, acceleratorStoreAndLangSelectStep2_FORM);
				
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting acceleratorStoreAndLangSelectStep2");
	}
	
	private void acceleratorStoreAndLangSelectStep3() throws TaskFailException{	
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Entering acceleratorStoreAndLangSelectStep3");
		
		WebHit("https://standardhost/webapp/wcs/tools/servlet/tools/common/AcceleratorHome.jsp", WEB_FORM_GET);
		
		Log.finest("Task["+this.getClass().getSimpleName()+"]: Exiting acceleratorStoreAndLangSelectStep3");
	}
	
}
